

$(function()
{
	$( "#productcategorylist-0" ).nestedSortable({
		listType: "ul",
		handle: 'div',
        items: 'li',
        toleranceElement: '> div',
        placeholder: 'placeholder',
        forcePlaceholderSize: true,
		helper:	'clone',
		revert: 250,
		isTree: true,
		expandOnHover: 700,
		startCollapsed: true,
		stop: function(event, ui)
		{
			$parent = $(ui.item).parent().parent();
			console.log($parent);
			if($parent.size() == 0)
			{
				$parent = $(ui.item).parent();
				$parent.data("productcategoryid","0");

				$children = $parent.find("> li").map(function(i, el)
				{
					return parseInt($(el).data("productcategoryid"));

				}).get();
			}
			else
			{
				$children = $parent.find("> ul > li").map(function(i, el)
				{
					return parseInt($(el).data("productcategoryid"));

				}).get();
				console.log($children);	
			}

			var parentid = parseInt($parent.data("productcategoryid"));
			parentid = (isNaN(parentid)?0:parentid);

			$.post("/manager/productcategories/move",
			{
				"parentid" : parentid,
				"children" : $children
			}, function(data)
			{
				
			}, "json");
		}
	});

	$(".delete-productcategory").submit(function(e){
		return confirm("WARNING: This action will DELETE this category and any CHILD CATEGORIES as well!\n\nAre you sure you want to proceed?");
	})

	$("#name.newproductcategory").blur(function(event) {
		$.post("/manager/productcategories/getslug", {
			"str":$(this).val()
		}, function(data)
		{
			$("#slug").val(data.val);
		})
	});
});